﻿using Cart.HoaDonSrv;
using Cart.NguoiDungSrv;
using Cart.SanPhamSrv;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Cart
{
    public partial class MuaHang : VdcInc.vdcAJAXPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            LoadGridGioHang();

            var user = Session["nsd"] as NguoiDung;
            if (user == null)
            {
                plHolder.Controls.Add(LoadControl("~/LoginRegister.ascx"));
            }


            

            

        }
        public void LoadGridGioHang()
        {
            var giohang = Session["listGioHang"] as List<SanPham>;
            if (giohang != null)
            {
                long longTongSoLuong = 0;
                long longTongTien = 0;
                foreach (var sanpham in giohang)
                {
                    longTongSoLuong += sanpham.SoLuongMua;
                    longTongTien += sanpham.TongSoTien;
                }
                lbTongSanPham.InnerText = longTongSoLuong.ToString();
                lbTongTien.InnerText = longTongTien.ToString();
                lbTongTien1.InnerText = longTongTien.ToString();
            }
            else
            {
                lbTongSanPham.InnerText = "0";
                lbTongTien.InnerText = "0";
                lbTongTien1.InnerText = "0";
            }



            var lst = Session["listGioHang"] as List<SanPham>;
            lvDanhSach.DataSource = lst;
            lvDanhSach.DataBind();
        }

        public string vDatMua()
        {
            using (var hService = new HoaDonSrv.HoaDonServiceClient())
            {

                var lst = Session["listGioHang"] as List<SanPham>;
                if (lst!=null)
                {
                    var user = Session["nsd"] as NguoiDung;
                    if (user != null)
                    {
                        var hd = new HoaDon();
                        long longTongTien = 0;
                        var listChiTiet = new List<HoaDonChiTiet>();
                        foreach (var sanpham in lst)
                        {
                            
                            var chitiet = new HoaDonChiTiet();
                            chitiet.SanPhamId = sanpham.Id;
                            chitiet.GiaTien = sanpham.Gia;
                            chitiet.SoLuong = sanpham.SoLuongMua;
                            chitiet.TrangThai = 0;
                            listChiTiet.Add(chitiet);
                            
                            longTongTien += sanpham.Gia * sanpham.SoLuongMua;
                        }
                        hd.lstChiTiet = listChiTiet;
                        hd.NguoiDungId = user.Id;
                        hd.TongTien = longTongTien;
                        hd.TrangThai = 0;


                        var result = hService.CapNhatHoaDon(hd);
                        Session["listGioHang"] = null;
                        return "Lập đơn hàng : " + result;
                    }
                    return "Chưa đăng nhập";
                }
                return "Bạn chưa chọn mua sản phẩm nào";
            }
            
        }

        public Dictionary<string, string> AddDataSource()
        {
            Dictionary<string, string> list = new Dictionary<string, string>();
            for (int i = 1; i <= 10; i++)
            {
                list.Add(i.ToString(), i.ToString());

            } return list;
        }

        public string vThayDoiSoLuong(string strSanphamid, string strSoLuongSanPham)
        {
            var lst = Session["listGioHang"] as List<SanPham>;
            if (lst != null)
            {
                foreach (var sanpham in lst)
                {
                    if (sanpham.Id == Convert.ToInt64(strSanphamid))
                    {
                        sanpham.SoLuongMua = Convert.ToInt32(strSoLuongSanPham);
                        sanpham.TongSoTien = sanpham.SoLuongMua * sanpham.Gia;
                    }
                }
                Session["listGioHang"] = lst;
            }
            LoadGridGioHang();
            UpdatePanel("cbpTongSanPham");
            UpdatePanel("cbpTongTien");
            //UpdatePanel("cbpGrid");
            return "";
        }

        public void vDelSanPham(string strSanphamid)
        {
            var lst = Session["listGioHang"] as List<SanPham>;
            if (lst != null)
            {
                var listnew = new List<SanPham>();
                foreach (var sp in lst)
                {
                    
                    if (sp.Id != Convert.ToInt64(strSanphamid))
                    {
                        listnew.Add(sp);
                    }
                }
                Session["listGioHang"] = listnew ;
            }
        }
    }
}